home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS08.ADF / C / Crunch.c < prev    next >
C/C++ Source or Header  |  1986-04-02  |  5KB  |  156 lines

  1. /**********************************************************
  2.  *               Crunch.c                                 *
  3.  *           by Larry Phillips.                           *
  4.  *                                                        *
  5.  *  USAGE: CRUNCH INFILE [TO] OUTFILE                     *
  6.  *  NOTE: Nothing fancy, use only above syntax            *
  7.  *                                                        *
  8.  *  Remove comments and unnecessary linefeeds from a      *
  9.  *  C source file.                                        *
  10.  *                                                        *
  11.  *  CREATED - 26 Dec. 85                                  *
  12.  *                                                        *
  13.  **********************************************************/
  14.  
  15. #define none 0
  16. #define lf 10
  17. #define space 32
  18. #define comment 1
  19. #define TRUE 1
  20. #define FALSE 0
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24.  
  25. main(argc, argv)
  26.  
  27. int argc;
  28. char *argv[];
  29.  
  30. {
  31. int keepgoing = 0;
  32. int flag = 0;
  33.  
  34. char c;
  35. FILE *in, *out;
  36.  
  37. if (argc < 3)
  38.   {  puts("Bad arguments.\n");
  39.      exit(22);
  40.   }
  41.  
  42. if ((in = fopen (argv [1], "r")) == 0)
  43.    { puts("Can't open inout file.\n");
  44.      exit(27);
  45.    }
  46.  
  47. if (argc == 4)
  48.    {
  49.    if ((out = fopen (argv [3], "w")) == 0)
  50.       { puts("Can't open output file.\n");
  51.       exit(32);
  52.       }
  53.  
  54.    }
  55.  
  56. if (argc == 3)
  57.    {
  58.    if ((out = fopen (argv [2], "w")) == 0)
  59.       { puts("Can't open output file.\n");
  60.       exit(32);
  61.       }
  62.    }
  63.  
  64. while((c = getc(in)) !=EOF)
  65.    {
  66.    keepgoing = TRUE;
  67.  
  68.    while (keepgoing == TRUE)           /* till character resolved */
  69.       {
  70.       if (flag == none)                /* check for special cases */
  71.          {
  72.  
  73.          if (c == '/')                 /* check for possible comment */
  74.             {
  75.             if ((c = getc(in)) == '*') /* is comment, set flag */
  76.                {
  77.                flag = comment;
  78.                c = 0;
  79.                keepgoing = FALSE;
  80.                }
  81.             else                       /* not comment, output '/'  */
  82.                putc ('/', out);        /* leave c as new character */
  83.             }                          /* new character will be    */
  84.                                        /* run through again        */
  85.  
  86.          if (c == '\n' || c == '\r')   /* set linefeed flag */
  87.             flag = lf;
  88.  
  89.          if (c == ' ' || c == '\t')    /* set space flag on space or tab */
  90.             flag = space;
  91.  
  92.          if (flag == none)             /* no flags to be set. drop out */
  93.             keepgoing = FALSE;         /* and put character to output  */
  94.  
  95.          }                             /* endif (flag = none) */
  96.  
  97. /*************************************************************************/
  98.  
  99.       if (flag == space)               /* if space flag on...    */
  100.          {
  101.          if (c == ' ' || c == '\t')    /* check for space or tab */
  102.             {
  103.             c = 0;                     /* yes... don't output    */
  104.             keepgoing = FALSE;
  105.             }
  106.          else
  107.             {
  108.             putc (' ', out);           /* no... output space and leave */
  109.             flag = none;               /* c as new character to be run */
  110.             }                          /* through again.               */
  111.          }
  112.  
  113.       if (flag == lf)
  114.          {
  115.          if (c == '\n' || c == ' ' || c == '\r') /*  strip extra        */
  116.             {                                    /* linefeeds, tabs or  */
  117.             c = 0;                              /* spaces after a '\n' */
  118.             keepgoing = FALSE;
  119.             }
  120.          else
  121.             {
  122.             putc ('\n', out);    /* output 1 linefeed when printing char */
  123.             flag = none;         /* is received. c saves new char        */
  124.             }
  125.          }
  126.  
  127.       if (flag == comment)       /* process comments */
  128.          {
  129.          if (c == '*')
  130.             {
  131.             if ((c = getc(in)) == '/')  /* if end of comment             */
  132.                {
  133.                flag = none;             /* turn off comment flag         */
  134.                c = 0;                   /* we don't have next character  */
  135.                keepgoing = FALSE;       /* so go get next one            */
  136.                }
  137.             }
  138.          else
  139.             {
  140.             c = 0;
  141.             keepgoing = FALSE;
  142.             }
  143.          }
  144.       }                                /*  endwhile  keepgoing  */ 
  145.    if (c != 0)
  146.       putc (c, out);
  147.    }                                   /*  endwhile NOT EOF  */
  148. putc('\n', out);
  149. fclose (in);
  150. fclose (out);
  151.  
  152. }                   /* end of program  */
  153.  
  154.  
  155.  
  156.